package socket.myRPC.util;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

/**
 * 表示 序列化包
 *
 * @author lihh
 */
public class SerDerUtil {
  static ByteArrayOutputStream out = new ByteArrayOutputStream();
  
  public synchronized static byte[] serialize(Object msg) {
    // 清空 buffer 中残漏的信息
    out.reset();
    
    ObjectOutputStream oout;
    byte[] msgBody = null;
    try {
      // 这是 对象输出流
      oout = new ObjectOutputStream(out);
      // 将 msg 写入到 对象输出流
      oout.writeObject(msg);
      // 转 字节数组
      msgBody = out.toByteArray();
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    return msgBody;
  }
}
